package com.oritsh.dicom.service;

import com.oritsh.dicom.exception.DicomException;
import com.oritsh.dicom.scu.DicomStreamStore;
import com.oritsh.dicom.scu.GetSCU;
import com.oritsh.dicom.utils.DataSetHelper;
import com.oritsh.dicom.utils.SCUFactory;
import org.dcm4che.data.Attributes;
import org.dcm4che.data.Tag;
import org.dcm4che.io.DicomInputStream;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: zarra
 * Date: 14-2-9
 * Time: 下午12:43
 * Copyright Shanghai Orient Rain Information Technology Co.,Ltd.
 */
public class CGetService {
    SCUFactory scuFactory;

    DicomStreamStore dicomStreamStore;

    public DicomInputStream cget(String study,String series,String sop) throws DicomException, IOException {
        DicomInputStream inputStream = dicomStreamStore.fetch(sop);
        if (inputStream != null)
            return inputStream;
        try(GetSCU scu = (GetSCU) scuFactory.createSCU(GetSCU.class)){
            scu.setDicomStreamStore(dicomStreamStore);
            Attributes attributes = new Attributes();
            DataSetHelper.putValueToDataSet(attributes, Tag.StudyInstanceUID, study);
            DataSetHelper.putValueToDataSet(attributes,Tag.SeriesInstanceUID,series);
            DataSetHelper.putValueToDataSet(attributes,Tag.SOPInstanceUID,sop);
            DataSetHelper.putValueToDataSet(attributes, Tag.QueryRetrieveLevel, "IMAGE");
            return scu.get(attributes);
        }
    }

    public SCUFactory getScuFactory() {
        return scuFactory;
    }

    public void setScuFactory(SCUFactory scuFactory) {
        this.scuFactory = scuFactory;
    }

    public DicomStreamStore getDicomStreamStore() {
        return dicomStreamStore;
    }

    public void setDicomStreamStore(DicomStreamStore dicomStreamStore) {
        this.dicomStreamStore = dicomStreamStore;
    }
}
